package com.css.fxfzfxqh.modules.comprehensiveSearch.repository.impl;

import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.CalculationTasks;
import com.css.fxfzfxqh.modules.comprehensiveSearch.dto.CalculationResultListDto;
import com.css.fxfzfxqh.modules.comprehensiveSearch.entity.CalculationResult;
import com.css.fxfzfxqh.modules.comprehensiveSearch.param.FuzzyQueryParam;
import com.css.fxfzfxqh.modules.comprehensiveSearch.param.QueryParam;
import com.css.fxfzfxqh.modules.comprehensiveSearch.repository.CalculationResultRepository;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/06/20
 * @Description: 查询和导出计算结果Repository
 */
@Repository
public class CalculationResultRepositoryImpl implements CalculationResultRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public List<CalculationResultListDto> queryAssess(String tableName) {
        StringBuilder sql = new StringBuilder("select * from " + tableName);
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        sql.append(whereSql).append(orderSql);
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CalculationResultListDto.class));
    }

    @Override
    public CalculationResultListDto getById(String tableName, String id) {
        try {
            StringBuilder sql = new StringBuilder("select * from " + tableName);
            StringBuilder whereSql = new StringBuilder(" where del_flag = '0' and id = '" + id + "'");
            StringBuilder orderSql = new StringBuilder(" order by create_time desc limit 1 offset 1");
            sql.append(whereSql).append(orderSql);
            return jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(CalculationResultListDto.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }


    @Override
    public Map<String, Object> queryCalculationTasks(int curPage, int pageSize, QueryParam param) {
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        //计算进度
        StringBuilder progress = new StringBuilder();
        StringBuilder sql = new StringBuilder("select b.* from  ");
        sql.append(" (SELECT assess_id FROM fxqh_assess_toponym where 1=1 ");
        //如果省市县不为null则
        if (!PlatformObjectUtils.isEmpty(param.getProvince())) {
            sql.append(" and pp_name like '%" + param.getProvince() + "%' ");
        }
        if (!PlatformObjectUtils.isEmpty(param.getCity())) {
            sql.append(" and city_name like '%" + param.getCity() + "%' ");
        }
        if (!PlatformObjectUtils.isEmpty(param.getCounty())) {
            sql.append(" and name like '%" + param.getCounty() + "%' ");
        }
        sql.append("group by assess_id) a, ");
        sql.append(" (select * from fxqh_calculation_tasks where 1=1 ");
        //如果为综合评估
        if (!PlatformObjectUtils.isEmpty(param.getQhType())&&"1".equals(param.getQhType())){
            if (!PlatformObjectUtils.isEmpty(param.getQhType())){
                if (param.getSeismicInputMode().equals("0")) {
                    progress.append("and zhzbqh_ld_progress = 1 ");
                }
                if (param.getSeismicInputMode().equals("1")) {
                    progress.append("and zhzbqh_four_progress = 1 ");
                }
                if (param.getSeismicInputMode().equals("2")) {
                    progress.append("and zhzbqh_field_progress = 1 ");
                }
                if (param.getSeismicInputMode().equals("3")) {
                    progress.append("and zhzbqh_occurrence_progress = 1 ");
                }
                if (param.getSeismicInputMode().equals("4")) {
                    progress.append("and zhzbqh_go_beyond_progress = 1 ");
                }
            }
        }else {
            //区划方式 0为单指标区划,1为综合指标区划
            if (!PlatformObjectUtils.isEmpty(param.getQhType())){
                if (param.getQhType().equals("0")){
                    progress.append("dzbqh_");
                }
            }
            //评估方式 0确定评估 1概率评估
            if (!PlatformObjectUtils.isEmpty(param.getAssessType())) {
                sql.append(" and assess_type = '" + param.getAssessType() + "'");
                //评估对象0为结构破坏,1为人员受伤,2为人员死亡,3为经济损失
                if (!PlatformObjectUtils.isEmpty(param.getAssessObj())) {
                    if ("2".equals(param.getAssessObj())){
                        sql.append(" and assess_obj like '%1%'");
                    }else  if ("3".equals(param.getAssessObj())){
                        sql.append(" and assess_obj like '%2%'");
                    }else {
                        sql.append(" and assess_obj like '%" + param.getAssessObj() + "%'");
                    }
                    if("1".equals(param.getAssessType())){
                        param.setSeismicInputMode("3");
                    }
                    if (!PlatformObjectUtils.isEmpty(param.getSeismicInputMode())) {
                        //地震输入方式0为设定烈度1为四个概率,2为地震影响场,  3为发生概率危险性,4为超越概率危险性
                        if(!"1".equals(param.getAssessType())){
                            sql.append(" and seismic_input_mode like '%" + param.getSeismicInputMode() + "%'");
                        }
                        if (param.getAssessObj().equals("0")) {
                            if (param.getSeismicInputMode().equals("0")) {
                                progress.append("structural_ld_progress = '1' ");
                            }
                            if (param.getSeismicInputMode().equals("1")) {
                                progress.append("structural_four_progress= '1' ");
                            }
                            if (param.getSeismicInputMode().equals("2")) {
                                progress.append("structural_field_progress= '1' ");
                            }
                            if("1".equals(param.getAssessType())){
                                progress.append("(structural_occurrence_progress= '1' or structural_go_beyond_progress= '1' ) ");
                            }else{
                                if (param.getSeismicInputMode().equals("3")) {
                                    progress.append("structural_occurrence_progress= '1' ");
                                }
                                if (param.getSeismicInputMode().equals("4")) {
                                    progress.append("structural_go_beyond_progress= '1' ");
                                }
                            }

                        }
                        if (param.getAssessObj().equals("1")||"2".equals(param.getAssessObj())) {
                            if (param.getSeismicInputMode().equals("0")) {
                                progress.append("casualties_ld_progress= '1' ");
                            }
                            if (param.getSeismicInputMode().equals("1")) {
                                progress.append("casualties_four_progress= '1' ");
                            }
                            if (param.getSeismicInputMode().equals("2")) {
                                progress.append("casualties_field_progress= '1' ");
                            }
                            if("1".equals(param.getAssessType())){
                                progress.append(" (casualties_occurrence_progress= '1' or  casualties_go_beyond_progress= '1') ");
                            }else{
                                if (param.getSeismicInputMode().equals("3")) {
                                    progress.append("casualties_occurrence_progress= '1' ");
                                }
                                if (param.getSeismicInputMode().equals("4")) {
                                    progress.append("casualties_go_beyond_progress= '1' ");
                                }
                            }
                        }
                        if (param.getAssessObj().equals("3")) {
                            if (param.getSeismicInputMode().equals("0")) {
                                progress.append("loss_ld_progress= '1' ");
                            }
                            if (param.getSeismicInputMode().equals("1")) {
                                progress.append("loss_four_progress= '1' ");
                                if (param.getSeismicInputMode().equals("2")) {
                            }
                                progress.append("loss_field_progress= '1' ");
                            }
                            if("1".equals(param.getAssessType())){
                                progress.append(" (loss_occurrence_progress= '1' or  loss_go_beyond_progress= '1') ");
                            }else{
                                if (param.getSeismicInputMode().equals("3")) {
                                    progress.append("loss_occurrence_progress= '1' ");
                                }
                                if (param.getSeismicInputMode().equals("4")) {
                                    progress.append("loss_go_beyond_progress= '1' ");
                                }
                            }

                        }
                        //sql.append(" and "+progress.toString()+" = '1' ");
                        sql.append(" and "+progress.toString()+" ");
                    }
                }
            }
        }
        sql.append(" )b  where a.assess_id=b.id ");
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);

        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CalculationTasks.class)).size();
        List<CalculationTasks> list = jdbcTemplate.query(sql.append(orderSql).append(limitSql).toString(), new BeanPropertyRowMapper<>(CalculationTasks.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public Map<String, Object> queryCalculationTasksByLike(int curPage, int pageSize, FuzzyQueryParam param) {
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder sql = new StringBuilder("select b.* from  ");
        sql.append(" (SELECT assess_id FROM fxqh_assess_toponym where 1=1 ");
        //如果省市县不为null则
        if (!PlatformObjectUtils.isEmpty(param.getPcc())) {
            sql.append(" and pp_name like '%" + param.getPcc() + "%'");
            sql.append(" or city_name like '%" + param.getPcc() + "%'");
            sql.append(" or name like '%" + param.getPcc() + "%'");
        }
        sql.append("group by assess_id) a, ");
        sql.append("(select * from fxqh_calculation_tasks where 1=1 ");
        //评估记录编号
        if (!PlatformObjectUtils.isEmpty(param.getAssessCode())) {
            sql.append(" and assess_code like '%" + param.getAssessCode() + "%'");
        }
//        if (!PlatformObjectUtils.isEmpty(param.getAssessDesc())) {
//            sql.append(" and assess_desc like '%" + param.getAssessDesc() + "%'");
//        }
        if (!PlatformObjectUtils.isEmpty(param.getCreateUserName())) {
            sql.append(" and create_user_name like '%" + param.getCreateUserName() + "%'");
        }
        sql.append(" )b  where a.assess_id=b.id ");
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);

        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CalculationTasks.class)).size();
        List<CalculationTasks> list = jdbcTemplate.query(sql.append(orderSql).append(limitSql).toString(), new BeanPropertyRowMapper<>(CalculationTasks.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }
}
